package leetcode.q203;

import leetcode.ListNode;

/**
 * <a href="https://leetcode.cn/problems/remove-linked-list-elements/description/">...</a>
 * 203. 移除链表元素
 *  使用 dummyHead
 */
public class Solution2 {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) return head;
        // 链接头节点
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode prev = dummyHead;
        while (prev.next != null) {
            if (prev.next.val == val) {
                ListNode node = prev.next;
                prev.next = node.next;
                node.next = null;
                continue;
            }
            prev = prev.next;
        }

        return dummyHead.next;
    }
}